package com.wn.excel.convert;

import cn.hutool.core.util.StrUtil;
import com.wn.excel.config.TableConvertProperties;
import com.wn.excel.constant.ExcelConst;
import com.wn.excel.model.Column;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @FileName: DefaultTypeConvert
 * @Description: oracle->mysql 默认格式转换器
 * @Author: MaWenHao
 * @Created: 2020/7/21 - 9:21
 * @Version: V1.0
 * @Company: 东方微银科技（北京）有限公司
 */
@Slf4j
@Component
public class DefaultDataTypeGenerator implements DataTypeDescGenerator {

    @Autowired
    private TableConvertProperties convertProperties;

    /**
     * 生成数据类型描述
     *
     * @param column
     * @return
     */
    @Override
    public String gen(Column column) {
        if (this.isNonDataSizeDesc(column.getDataType())) {
            return column.getDataType();
        }
        StringBuilder sb = new StringBuilder();
        if (StringUtils.isEmpty(column.getDataPrecision()) || StringUtils.isEmpty(column.getDataScale())) {
            return sb.append(column.getDataType())
                    .append(ExcelConst.LEFT_BRACKET)
                    .append(column.getDataLength())
                    .append(ExcelConst.RIGHT_BRACKET)
                    .toString();
        }
        return sb.append(column.getDataType())
                .append(ExcelConst.LEFT_BRACKET)
                .append(column.getDataPrecision())
                .append(ExcelConst.COMMA)
                .append(column.getDataScale())
                .append(ExcelConst.RIGHT_BRACKET)
                .toString();
    }

    private boolean isNonDataSizeDesc(String dataType) {
        return convertProperties.getNonLengthList().stream().anyMatch(type -> StrUtil.equals(type, dataType));
    }
}
